<template>
  <router-view :key="$route.fullPath" />
</template>

<script setup>
import useSettingsStore from '@/store/modules/settings'
import {handleThemeStyle} from '@/utils/theme'
import {useWebSocketStore} from "@/store/modules/websocket.js";
import useUserStore from '@/store/modules/user.js';
import {watch, onMounted, nextTick} from 'vue'

const userStore = useUserStore()
const wsStore = useWebSocketStore()
// 监听登录状态变化
watch(
    () => userStore.token,
    async (loggedIn) => {
      if (loggedIn) {
        try {
          await userStore.getInfo()

          // 清除旧连接
          if (wsStore.stompClient?.connected) {
            await wsStore.disconnect()
          }

          // 初始化新连接
          await wsStore.initConnection()

          console.log('🔌 WebSocket 连接成功')
        } catch (error) {
          console.error('连接失败:', error)
        }
      }
    },
    {immediate: true}
)

onMounted(() => {
  nextTick(() => {
    // 初始化主题样式
    handleThemeStyle(useSettingsStore().theme)
  })
})
</script>
